package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.ReservationDto;
import com.zzyl.entity.Reservation;
import com.zzyl.mapper.CustomerReservationMapper;
import com.zzyl.service.CustomerReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.ReservationCountVo;
import com.zzyl.vo.ReservationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Service
public class CustomerReservationServiceImpl implements CustomerReservationService {
    @Autowired
    private CustomerReservationMapper customerReservationMapper;

    /**
     * 查询取消预约数量
     * @return
     */
    @Override
    public Integer searchCount() {
       return customerReservationMapper.searchCount();
    }

    /**
     * 统计该用户的在今天的取消次数
     * @return
     */
    @Override
    public Integer cancelCount(){
        Long userId =Long.parseLong(UserThreadLocal.getSubject());
        LocalDateTime startTime = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0);
        LocalDateTime endTime = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59);
        Integer count = customerReservationMapper.canceCount(startTime,endTime,userId);
        return count;
    };


    /**
     * 查询每个时间段剩余预约次数
     * @param time
     * @return
     */
    @Override
    public List<ReservationCountVo> getRemainCount(Long time) {
        Long endTime = time + 1000*60*60*24;
        Date endDate = new Date(endTime);
        Date date = new Date(time);
//        Instant instant = Instant.ofEpochMilli(time);
//        LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
//        return customerReservationMapper.getRemindCount(dateTime,dateTime.plusHours(24));
        return customerReservationMapper.getRemindCount(date,endDate);
    }

    /**
     * 新增预约信息
     *
     * @param reservationDto
     */
    @Override
    public void addInfo(ReservationDto reservationDto) {
        Reservation bean = BeanUtil.toBean(reservationDto, Reservation.class);
        Long id = Long.parseLong(UserThreadLocal.getSubject());
        bean.setCreateBy(id);
        bean.setUpdateBy(id);
        bean.setStatus(0);
        bean.setCreateTime(LocalDateTime.now());
        bean.setUpdateTime(LocalDateTime.now());
        customerReservationMapper.addInfo(bean);
    }

    /**
     * 分页查询预约
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<ReservationVo> getInfo(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<Reservation> page = customerReservationMapper.getById(status);
        return PageResponse.of(page,ReservationVo.class);
    }

    /**
     * 取消预约
     */
    @Override
    public void cancle(Long id) {
        customerReservationMapper.cancel(id);
    }
    /**
     * 取消预约
     * 更新status= 2 完成冗余字段的封装
     * @param id
     */
    @Override
    public void cancelByStatus(Long id){
        //获取该请求Threadlocal中的usrId
        Long userId =Long.parseLong(UserThreadLocal.getSubject());
        //封装
        Reservation reservation = new Reservation();
        reservation.setUpdateBy(id);
        reservation.setUpdateBy(userId);
        reservation.setUpdateTime(LocalDateTime.now());
        // TODO为什么不在sql中完成
        reservation.setStatus(2);
        //动态sql更新
        customerReservationMapper.update(reservation);
    }
}
